package ch.bfh.swos.learnaid.service;

import javax.inject.Inject;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import ch.bfh.swos.learnaid.service.dto.AuthorDTO;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/serviceContext.xml")
public class AuthorServiceIT {

    @Inject
    private AuthorService authorService;

    @Test
    public void test() {
        // Create
        AuthorDTO newAuthor = new AuthorDTO();
        newAuthor.setFirstname("Firstname");
        newAuthor = authorService.create(newAuthor);

        // Read
        AuthorDTO readAuthor = authorService.read(newAuthor.getId());
        Assert.assertTrue(newAuthor.getFirstname().equals(readAuthor.getFirstname()));

        // Update
        readAuthor.setFirstname("Changed");
        readAuthor = authorService.update(readAuthor);
        AuthorDTO updatedAuthor = authorService.read(readAuthor.getId());
        Assert.assertTrue(updatedAuthor.getFirstname().equals(readAuthor.getFirstname()));
        
        // Delete
        authorService.delete(updatedAuthor);
        AuthorDTO deletedAuthorDTO = authorService.read(updatedAuthor.getId());
        Assert.assertNull(deletedAuthorDTO);
    }

}
